package product

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func info(r *gin.RouterGroup) {
	info := r.Group("/info")
	{
		info.GET(sql.RequestList, productInfoList)
		info.GET(sql.RequestData, productInfoData)
		info.GET(sql.RequestDelete, productInfoDelete)
		info.POST(sql.RequestAdd, productInfoAdd)
		info.POST(sql.RequestUpdate, productInfoUpdate)
	}
}

func productInfoAdd(c *gin.Context) {
	productInfoEdit(sql.ADD, c)
}

func productInfoUpdate(c *gin.Context) {
	productInfoEdit(sql.UPDATE, c)
}

func productInfoData(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var product model.Product
		id := c.Query("id")
		db.Where("id = ?", id).
			Preload("ProductDetail", func(db *gorm.DB) *gorm.DB {
				db = db.Preload("FileInfo", func(db *gorm.DB) *gorm.DB {
					db = db.Where("type = ?", "product")
					return db
				})
				return db
			}).
			Preload("FileInfo", func(db *gorm.DB) *gorm.DB {
				db = db.Where("type = ?", "product")
				return db
			}).
			First(&product)
		return db, product
	})
}

func productInfoDelete(c *gin.Context) {
	sql.HttpHandle(sql.DELETE, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var productId = c.Query("id")
		if err := db.Transaction(func(tx *gorm.DB) error {
			var salesStatisticsProduct []model.SalesStatisticsProduct
			if err := tx.Where("product_id = ?", productId).Find(&salesStatisticsProduct).Error; err != nil {
				return errors.New("存在关联数据，删除失败")
			}
			var inventorySalesProduct []model.InventorySalesProduct
			if err := tx.Where("product_id = ?", productId).Find(&inventorySalesProduct).Error; err != nil {
				return errors.New("存在关联数据，删除失败")
			}
			if err := tx.Where("id = ?", productId).Delete(&model.Product{}); err != nil {
				return errors.New("删除失败")
			}
			return nil
		}); err != nil {
			db.Error = err
			return db, nil
		}
		return db, nil
	})
}

func productInfoList(c *gin.Context) {
	sql.HttpList(c, &[]model.Product{}, func(db *gorm.DB) *gorm.DB {
		var search = c.Query("search")
		if search != "" {
			db = db.Where("CONCAT(REPLACE(factory_no,'.',''),product_name) LIKE ?", "%"+search+"%").
				Or("product_no LIKE ?", "%"+search+"%")
		}
		db = db.Preload("FileInfo", func(db *gorm.DB) *gorm.DB {
			db = db.Where("type = ?", "product")
			return db
		}).Order("id desc")
		return db
	})
}

func productInfoEdit(text string, c *gin.Context) {
	sql.HttpHandle(text, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var product model.Product
		if err := sql.PostParamsBind(c, db, &product).Error; err != nil {
			return db, nil
		}
		if text == sql.ADD && product.Id != 0 {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		} else if text == sql.UPDATE && product.Id == 0 {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		if err := db.Transaction(func(tx *gorm.DB) error {
			var oldProductList []model.Product
			if err := tx.Where("id != ? AND factory_no = ? AND product_name = ? AND nation = ?",
				product.Id, product.FactoryNo, product.ProductName, product.Nation).
				Find(&oldProductList).Error; err != nil {
				return err
			}
			if len(oldProductList) > 0 {
				return errors.New("商品已存在")
			}
			if text == sql.ADD {
				if err := tx.Create(&product).Error; err != nil {
					return err
				}
			} else if text == sql.UPDATE {
				if err := tx.Updates(&product).Error; err != nil {
					return err
				}
			}
			return nil
		}); err != nil {
			db.Error = err
			return db, nil
		}
		return db, product
	})
}
